package cn.stard.org.controller;

import cn.stard.basic.annotation.PreAuthorize;
import cn.stard.basic.util.LoginContext;
import cn.stard.org.query.EmployeeQuery;
import cn.stard.basic.util.AjaxResult;
import cn.stard.basic.util.PageList;
import cn.stard.org.domain.Employee;
import cn.stard.org.service.IEmployeeservice;
import cn.stard.pet.domain.Pet;
import cn.stard.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/*复合注解拥有两个的所有功能*/
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private IEmployeeservice service;


        /*
        * @Title:
        * @Description: 通过id 查询数据
        * @Author: Mr.She
        * @Version: 1.0
        * @Date:
        * @Parameters:
        * @Return
        */

    @GetMapping// 请求地址：http://localhost:8080/employee GET请求
    public List<Employee> loadAll(){

        return service.loadAll();

    }

    @GetMapping("/{id}")
    @PreAuthorize(name="通过id查询", sn = "employee:loadById")
    public Employee findeEmployee(@PathVariable("id")Long id){
        Employee employee = service.loadById(id);

        return employee;
    }
    // 分页查询@RequestBody 请求获取参数
    @PostMapping
    @PreAuthorize(name="员工分页查询", sn = "employee:list")
    public PageList<Employee> list(@RequestBody EmployeeQuery employeeQuery){
        System.out.println("进入查询");
        System.out.println(employeeQuery);
        PageList<Employee> data = service.queryData(employeeQuery);
        return data;
    }

    @DeleteMapping("/{id}")
    @PreAuthorize(name="员工删除", sn = "employee:remove")
    public AjaxResult remove(@PathVariable("id")Long id){
        try {
            System.out.println("执行1");
            service.remove(id);
            return  AjaxResult.me();
        }catch (Exception e){
            System.out.println("执行2");
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙");
        }


    }


    @PatchMapping
    @PreAuthorize(name="员工批量删除", sn = "employee:deles")
    public AjaxResult deles(@RequestBody List<Long> ids){
        try {

            service.deles(ids);
            return  AjaxResult.me();
        }catch (Exception e){
            System.out.println("执行2");
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙");
        }


    }




    @PutMapping
    @PreAuthorize(name="员工添加修改", sn = "employee:addorupt")
    public AjaxResult addorupt(@RequestBody  Employee employee){
        try {
            if (employee.getId()==null){
                System.out.println(employee);
                service.save(employee);

            }else {
                service.update(employee);
            }
            return  AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙");
        }

    }

    @GetMapping("/list/{shopId}")
    public List<Employee> loadByShopId(@PathVariable("shopId") Long shopId){
        return service.loadByShopId(shopId);
    }


//    @PutMapping
//    public AjaxResult addOrUpdate(@RequestBody Employee employee1, HttpServletRequest req){
//        try {
//            // 通过工具类来获取登录用户id
//            Logininfo Logininfo = LoginContext.getLogininfo(req);
//            // 通过用户id来获取用户对象
//            Employee employee = service.loadByloginfoId(Logininfo.getId());
//            // 通过用户获取到店铺id
//            Long shopId = employee.getShopId();
//            // 把店铺id添加到里面
//            employee1.setShopId(shopId);
//            if( employee1.getId()!=null)
//                service.update(employee1);
//            else
//                service.save(employee1);
//            return AjaxResult.me();
//        } catch (Exception e) {
//            e.printStackTrace();
//            return AjaxResult.me().setMessage("保存对象失败！"+e.getMessage());
//        }
//    }

}
